package com.lcq.study.javaassist;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.bytecode.AccessFlag;
import javassist.bytecode.AttributeInfo;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @ClassName JavaAssistTest1
 * @Description
 * @Author lichuanqi
 * @Date 2021/11/18 15:19
 * @Version 1.0
 **/
public class JavaAssistTest1 {
    public static void main(String[] args) {
        ClassPool classPool = new ClassPool();
        classPool.appendSystemPath();
        try {
            CtClass ctClass = classPool.get("com.lcq.study.javabase.Company");
            CtMethod[] methods = ctClass.getMethods();
            System.out.println(methods[0].getName());
            System.out.println(methods[0].getReturnType().getName());
            System.out.println(getModifierName(methods[0].getModifiers()));
            System.out.println(Arrays.stream(methods[0].getParameterTypes()).map(aClass->{
                return aClass.getName();
            }).collect(Collectors.toList()));
        } catch (NotFoundException e) {
            e.printStackTrace();
        }

    }

    public static String getModifierName(int modifiers){
        StringBuilder stringBuilder = new StringBuilder();
        if (AccessFlag.isPublic(modifiers)){
            stringBuilder.append("public ");
        }

        if (AccessFlag.isProtected(modifiers)){
            stringBuilder.append("protected ");
        }

        if (AccessFlag.isPrivate(modifiers)){
            stringBuilder.append("private ");
        }

        if ((modifiers & AccessFlag.STATIC) != 0){
            stringBuilder.append("static ");
        }

        if ((modifiers & AccessFlag.FINAL) != 0){
            stringBuilder.append("final ");
        }

        if ((modifiers & AccessFlag.SYNCHRONIZED) != 0){
            stringBuilder.append("synchronized ");
        }


        return stringBuilder.toString();
    }
}
